package com.youlai.system.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.model.Option;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.SourceForm;
import com.youlai.system.model.query.SourcePageQuery;
import com.youlai.system.model.vo.SourcePageVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SysSourceService;
import com.youlai.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 人员批次控制器
 */
@Tag(name = "13.人员批次接口")
@RestController
@RequestMapping("/api/v1/sources")
@RequiredArgsConstructor
public class SysSourceController {

    private final SysSourceService sourceService;

    private final SysUserService userService;

    @Operation(summary = "人员批次分页列表")
    @GetMapping("/page")
    public PageResult<SourcePageVO> getSourcePage(
            @ParameterObject SourcePageQuery queryParams
    ) {
        IPage<SourcePageVO> result = sourceService.getSourcePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增人员批次")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:source:add')")
    @PreventDuplicateSubmit
    public Result saveSource(
            @RequestBody @Valid SourceForm sourceForm
    ) {
        boolean result = sourceService.saveSource(sourceForm);
        return Result.judge(result);
    }

    @Operation(summary = "人员批次表单数据")
    @GetMapping("/{sourceId}/form")
    public Result<SourceForm> getSourceForm(
            @Parameter(description = "人员批次ID") @PathVariable Long sourceId
    ) {
        SourceForm formData = sourceService.getSourceForm(sourceId);
        return Result.success(formData);
    }


    @Operation(summary = "修改人员批次")
    @PutMapping(value = "/{sourceId}")
    @PreAuthorize("@ss.hasPerm('sys:source:edit')")
    public Result updateSource(
            @Parameter(description = "人员批次ID") @PathVariable Long sourceId,
            @RequestBody @Validated SourceForm sourceForm) {
        boolean result = sourceService.updateSource(sourceId, sourceForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除人员批次")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:source:delete')")
    public Result deleteSources(
            @Parameter(description = "人员批次ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "人员批次删除数据为空");
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean sourceHasUsed = userService.sourceHasUsed(idList);
        Assert.isTrue(!sourceHasUsed, "人员批次使用中，不能被删除");
        boolean result = sourceService.deleteSources(idList);
        return Result.judge(result);
    }

    @Operation(summary = "人员批次下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> listSourceOptions() {
        List<Option> list = sourceService.listSourceOptions();
        return Result.success(list);
    }


}
